1 using System;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 namespace ProceduralToolkit.Examples
6 {
7 public class BuildingGeneratorBase
8 {
9 public static MeshDraft GenerateFacades(List<Vector2> foundationPolygon, List<FacadeLayout> facadeLayouts)
10 {
11 var buildingDraft = new MeshDraft {name = "Building"};
12 for (int i = 0; i < foundationPolygon.Count; i++)
13 {
14 Vector3 a = foundationPolygon[i].ToVector3XZ();
15 Vector3 b = foundationPolygon.GetLooped(i + 1).ToVector3XZ();
16 var facadeLayout = facadeLayouts[i];
17 buildingDraft.Add(GenerateFacade(a, b, facadeLayout));
18 }
19 return buildingDraft;
20 }
21
22 private static MeshDraft GenerateFacade(Vector3 a, Vector3 b, FacadeLayout facadeLayout)
23 {
24 var facadeDraft = facadeLayout.GetMeshDraft();
25
26 Vector3 normal = Vector3.Cross(Vector3.up, (b - a).normalized).normalized;
27 var rotation = Quaternion.LookRotation(-normal);
28 facadeDraft.Rotate(rotation);
29 facadeDraft.Move(a);
30 return facadeDraft;
31 }
32
33 #region Horizontal
34
35 public static HorizontalLayout ConstructHorizontal(
36 List<Func<IFacadePanel>> constructors,
37 float height,
38 float panelWidth,
39 int count)
40 {
41 var horizontal = new HorizontalLayout {height = height};
42 for (int i = 0; i < count; i++)
43 {
44 IFacadePanel panel = constructors.GetRandom()();
45 panel.width = panelWidth;
46 horizontal.Add(panel);
47 }
48 return horizontal;
49 }
50
51 public static HorizontalLayout ConstructHorizontal(
52 List<Func<IFacadePanel>> constructors,
53 float height,
54 Func<int, float> getPanelWidth,
55 int count)
56 {
57 var horizontal = new HorizontalLayout {height = height};
58 for (int i = 0; i < count; i++)
59 {
60 IFacadePanel panel = constructors.GetRandom()();
61 panel.width = getPanelWidth(i);
62 horizontal.Add(panel);
63 }
64 return horizontal;
65 }
66
67 public static HorizontalLayout ConstructHorizontal(
68 Func<IFacadePanel> constructor,
69 float height,
70 float panelWidth,
71 int count)
72 {
73 var horizontal = new HorizontalLayout {height = height};
74 for (int i = 0; i < count; i++)
75 {
76 IFacadePanel panel = constructor();
77 panel.width = panelWidth;
78 horizontal.Add(panel);
79 }
80 return horizontal;
81 }
82
83 #endregion Horizontal
84
85 #region Vertical
86
87 public static VerticalLayout ConstructVertical(
88 List<Func<IFacadePanel>> constructors,
89 float width,
90 float panelHeight,
91 int count)
92 {
93 var vertical = new VerticalLayout {width = width};
94 for (int i = 0; i < count; i++)
95 {
96 IFacadePanel panel = constructors.GetRandom()();
97 panel.height = panelHeight;
98 vertical.Add(panel);
99 }
100 return vertical;
101 }
102
103 public static VerticalLayout ConstructVertical(
104 List<Func<IFacadePanel>> constructors,
105 float width,
106 Func<int, float> getPanelHeight,
107 int count)
108 {
109 var vertical = new VerticalLayout {width = width};
110 for (int i = 0; i < count; i++)
111 {
112 IFacadePanel panel = constructors.GetRandom()();
113 panel.height = getPanelHeight(i);
114 vertical.Add(panel);
115 }
116 return vertical;
117 }
118
119 public static VerticalLayout ConstructVertical(
120 Func<IFacadePanel> constructor,
121 float width,
122 float panelHeight,
123 int count)
124 {
125 var vertical = new VerticalLayout {width = width};
126 for (int i = 0; i < count; i++)
127 {
128 IFacadePanel panel = constructor();
129 panel.height = panelHeight;
130 vertical.Add(panel);
131 }
132 return vertical;
133 }
134
135 #endregion Vertical
136 }
137 }